(function ($) {
	$.fn.xtable = function(options){
		var ths = this;
		if(ths.length==0){$.log("error: Unable to read element "+ths.selector);return;}
		var xfn = $(ths.selector).data(ths.selector);
		if(xfn){ return xfn; }
		var defaults = {
			url:null,
			data:{},
			paging:null,
			page:1,
			rows:20,
			xdata:null,
			minus:0,
			width:null,
			height:null,
			resize:false,
			bodyHeight:null,
			format:function(row){},
			success:function(data){}
		};
		var ops = $.extend(defaults,options,{
			tpl:null,
			template:null,
			list:null,
			total:0
		});
		var inside = {
			init:function(){
				inside.template();
				inside.setPaging();
				inside.setSize();
				inside.load();
			},
			reinit:function(){
				inside.setPaging();
				inside.load();
			},
			template:function(){
				ops.tpl = ths.find(".xtemplate");
				ops.template = ops.tpl.html();
			},
			setPaging:function(){
				if(ops.paging){
					ops.data.page = ops.page<1?1:ops.page;
					ops.data.rows = ops.rows;
				}
			},
			setSize:function(){
				if(ops.height){
					ths.height(ops.height);
				}
				if(ops.bodyHeight){
					ths.find(".xbody").height(ops.bodyHeight);
				}
				if(ops.width){
					ths.width(ops.width);
				}
			},
			load:function(){
				if(ops.url){
					$.ajax({
		                url:ops.url,
		                data:ops.data,
		                type:'POST',
		                dataType:'json',
		                success:inside.success,
		                error:inside.error
		            });
				}else if(ops.xdata){
					ops.list = ops.xdata.list;
					ops.total = ops.xdata.total;
					inside.draw();
				}
				if(ths.has(".xcheckbox")){
					ths.find(".xcheckbox").attr("checked",false);
				}
			},
			success:function(data){
				ops.list = data.list;
            	ops.total = data.total;
				inside.draw();
			},
			draw:function(){
            	ops.tpl.empty();
				ops.tpl.show();
				if(ops.list!=null)
            	$.each(ops.list,function(index,row){
            		row.index = index;
        			ops.format(row);
        			ops.tpl.append($.xtemplate(ops.template,row));
            	});
            	inside.resize();
            	inside.bind();
            	inside.initPaging();
            	inside.call();
			},
			error:function(e){
				$.error("数据加载失败！");
			},
			bind:function(){
				inside.bindCheckbox();
				inside.bindWindowResize();
			},
			bindCheckbox:function(){
				if(ths.has(".xcheckbox")){
					ths.find(".xcheckbox").change(function(){
						if($(this).is(":checked")){
							ths.find(".xtemplate input:checkbox").prop("checked",true);
						}else{
							ths.find(".xtemplate input:checkbox").prop("checked",false);
						}
					});
				}
			},
			bindWindowResize:function(){
				$(window).resize(function(){
					inside.resize();
				});
			},
			resize:function(){
				inside.bodyResize();
				inside.headResize();
			},
			headResize:function(){
				var xhead = ths.find(".xhead th");
				ops.tpl.find('tr:first td').each(function(index,item){
					if(index==xhead.length-1)return;
					$(item).width($(xhead[index]).width());				
				});
			},
			bodyResize:function(){
				if(ops.resize){
					var height = ths.parent().outerHeight()-ops.minus;
					ths.find(".xbody").css("max-height",height+"px");
				}
			},
			initPaging:function(){
				if(ops.paging){
					$(ops.paging).xpaging({
						page:ops.page,
						rows:ops.rows,
						total:ops.total,
						change:inside.loadPaging
					});
				}
			},
			loadPaging:function(data){
				ops.page = data.page;
				ops.rows = data.rows;
				inside.setPaging();
				inside.load();
			},
			call:function(){
        		ops.success({total:ops.total,list:ops.list});
			},
			reload:function(data){
				if(data){
					ops.data = $.extend(ops.data,data,{page:1});
					ops.page = 1;
				}
				inside.load();
			}
		};
		var xtable = function(){
			inside.init();
		}
		var table = new xtable();
		var outside = {
			setData:function(data){
				ops.list=data;
				inside.reinit();
			},
			getData:function(){
				return ops.list;
			},
			getRow:function(index){
				return ops.list[index];
			},
			reload:function(data){
				inside.reload(data);
			},
			getSelected:function(){
				var ids = "";
				ths.find(".xtemplate input:checkbox:checked").each(function(index,item){
					ids += $(item).attr("xval")+",";
				});
				if(ids != "")ids = ids.substring(0,ids.length-1);
				return ids;
			},
			setSelected:function(ids){
				$.each(ids,function(index,item){
					$(".xtemplate input:checkbox[xval='"+item+"']").attr("checked",true);
				});
			},
			clearSelected:function(){
				ths.find("input:checkbox").attr("checked",false);
			},
			setBodyHeight:function(height){
				ths.find(".xbody").css("height",height+"px");
			}
		};
		$.extend(table,outside);
		$(ths.selector).data(ths.selector,table);
		return table;
	};
	$.fn.xpaging = function(options){
		var ths = this;
		if(ths.length==0){$.log("error: Unable to read element "+ths.selector);return;}
		var defaults = {
			page:null,
			rows:null,
			total:null,
			change:function(e){$.log(e)}
		};
		var xdata = {
			page:null,
			rows:null,
			total:null,
			prev:null,
			next:null,
			first:null,
			last:null,
			size:null
		}
		var ops = $.extend(defaults,options);
		var inside = {
			init:function(){
				inside.template();
				inside.draw();
			},
			template:function(){
				if(!ths.data("template")){
					var tpl = ths.find(".xtemplate");
					var html = tpl.html();
					ths.data("template",html);
					tpl.empty();
				}
			},
			draw:function(){
				inside.setData();
				inside.setHtml();
				inside.bind();
			},
			setData:function(){
				xdata.page = ops.page;
				xdata.rows = ops.rows;
				xdata.total = ops.total;
				xdata.size = parseInt((xdata.total+xdata.rows-1)/xdata.rows);	
				xdata.prev = xdata.page>1?xdata.page-1:1;
				xdata.next = xdata.page<xdata.size?xdata.page+1:xdata.size;
				xdata.first = 1;
				xdata.last = xdata.size;
			},
			setHtml:function(){
				var html = $.xtemplate(ths.data("template"),xdata);
				var tpl = ths.find(".xtemplate");
				tpl.html(html);
				tpl.show();				
			},
			toPage:function(page){
				ops.page = page;
				inside.draw();
				inside.call();
			},
			setRows:function(rows){
				ops.rows = rows;
				inside.draw();
				inside.call();
			},
			bind:function(){
				ths.find(".xnext,.xprev,.xfirst,.xlast").click(function(){
					inside.toPage(parseInt($(this).attr("xval")));
				});
				ths.find(".xgo").click(function(){
					var xpage = $(this).parent().find(".xpage").val();
					var page = parseInt(xpage);
					if(isNaN(page)){
						$.error("请输入有效数字！");
					}else if(page<1||page>xdata.size){
						$.error("请输入有效范围！");
					}else{
						inside.toPage(page);
					}
				});
			},
			call:function(){
				ops.change({
					page:xdata.page,
					rows:xdata.rows
				});
			}
		}
		var xpaging = function(){
			inside.init();
		};
		var paging = new xpaging();
		var outside = {
			reload:function(){
			},
			setData:function(){
			}
		};
		$.extend(paging,outside);
		return paging;
	};
	$.fn.xlist = function(options) {
		var ths = this;
		if(ths.length==0){$.log("error: Unable to read element "+ths.selector);return;}
		var xfn = $(ths.selector).data(ths.selector);
		if(xfn){ return xfn; }
		var defaults = {
			url:null,
			data:{},
			page:1,
			rows:20,
			xdata:null,
			xload:".xload",
			xnodata:".xnodata",
			format:function(row){},
			success:function(data){}
		};
		var ops = $.extend(defaults,options,{
			tpl:null,
			template:null,
			list:null,
			total:0
		});
		var inside = {
			btnOps:null,
			init:function(){
				inside.loadTpl();
				inside.setPaging();
				inside.load();
			},
			loadTpl:function(){
				ops.tpl = ths.find(".xtemplate");
				ops.template = ops.tpl.html();
            	ops.tpl.empty();
				ops.tpl.show();
			},
			setPaging:function(){
				ops.data.page = ops.page<1?1:ops.page;
				ops.data.rows = ops.rows;
			},
			load:function(){
				if(ops.url){
					$.ajax({
		                url:ops.url,
		                data:ops.data,
		                type:'POST',
		                dataType:'json',
		                success:inside.success,
		                error:inside.error
		            });
				}else if(ops.xdata){
					ops.list = ops.xdata.list;
					ops.total = ops.xdata.total;
					inside.draw();
				}
			},
			reload:function(data){
				ops.tpl.empty();
				$.extend(ops.data,data,{page:1});
				inside.load();
			},
			error:function(){
				if(inside.btnOps){inside.btnOps.open();}
				$.error("数据加载失败！");
			},
			success:function(data){
				if(inside.btnOps){inside.btnOps.open();}
				ops.list = data.list;
            	ops.total = data.total;
				inside.draw();
			},
			draw:function(){
				if(ops.total>0){
					$(ops.xnodata).hide();
				}else{
					$(ops.xnodata).show();
				}
				if(ops.list!=null)
				$.each(ops.list,function(index,row){
					row.index = index;
					ops.format(row);
					var x = $.xtemplate(ops.template,row);
					ops.tpl.append(x);
				});
				inside.bind();
				inside.more();
				inside.call();
			},
			bind:function(){
				$(ops.xload).xbtn(function(btnOps){
					inside.btnOps = btnOps;
					btnOps.close();
					ops.data.page++;
					inside.load();
				});
			},
			more:function(){
				var size = parseInt((ops.total+ops.rows-1)/ops.rows);
				if(size>1&&ops.data.page<size){
					$(ops.xload).show();
				}else{
					$(ops.xload).hide();
				}
			},
			call:function(){
        		ops.success({total:ops.total,list:ops.list});
			},
			getRow:function(index){
				return ops.list[index];
			}
		};
		var xlist = function(){
			inside.init();
		};
		var list = new xlist();
		var outside = {
			reload:function(data){
				inside.reload(data);
			},
			getRow:function(index){
				return inside.getRow(index);
			}
		};
		$.extend(list,outside);
		$(ths.selector).data(ths.selector,list);
		return list;
	};
	$.fn.xtabs = function(options) {
		var ths = this;
		if(ths.length==0){$.log("error: Unable to read element "+ths.selector);return;}
		var xfn = $(ths.selector).data(ths.selector);
		if(xfn){ return xfn; }
		var defaults = {
			tab:0,
			width:null,
			height:null
		};
		var ops = $.extend(defaults,options);
		var inside = {
			btnOps:null,
			init:function(){
				inside.resize();
				inside.open(ops.tab);
				inside.bind();
			},
			resize:function(){
				if(ops.width){
					ths.width(ops.width);
				}
				if(ops.height){
					ths.height(ops.height);
				}
			},
			open:function(index){
				if(index!=ops.tab){
					ths.find(".tabs_head").children().eq(ops.tab).removeClass("selected");
					ths.find(".tabs_body").children().eq(ops.tab).hide();
					ops.tab = index;
				}
				ths.find(".tabs_head").children().eq(index).addClass("selected");
				ths.find(".tabs_body").children().eq(index).show();
			},
			bind:function(){
				inside.bindTab();
			},
			bindTab:function(){
				var headChildren = ths.find(".tabs_head").children();
				var bodyChildren = ths.find(".tabs_body").children();
				$.each(headChildren,function(index,item){
					$(item).click(function(){
						var ths = $(this);
						ths.parent().find(".selected").removeClass("selected");
						ths.addClass("selected");
						bodyChildren.eq(ops.tab).hide();
						bodyChildren.eq(index).show();
						ops.tab = index;
					});
				});
			}
		};
		var xtabs = function(){
			inside.init();
		};
		var tabs = new xtabs();
		var outside = {
			open:function(index){
				inside.open(index);
			},
			getOpen:function(){
				return ops.tab; 
			}
		};
		$.extend(tabs,outside);
		$(ths.selector).data(ths.selector,tabs);
		return tabs;
	};

	$.fn.xform = function (options) {
		var ths = this;
		var defaults = {
			url:'',
			method:'POST',
			dataType:'json',
			fileupload:false,
			semantic:false,
			success:function(){},
			error:function(){},
			valid:function(){return true;},
			beforeSend:function(){}
		};
		var ops = $.extend(defaults,options);
		var inside = {
			init:function(){
				var enctype = "multipart/form-data";
				if(ops.fileupload == true && ths.attr('enctype') != enctype){
					ths.attr("encoding", enctype);
				}
				if(ops.fileupload == false && ths.attr('enctype') == enctype){
					ops.fileupload = true;
				}
				inside.fileapi = $("<input type='file'/>").get(0).files !== undefined;
				inside.formdata = window.FormData !== undefined;
				inside.bind();
			},
			bind:function(){
				ths.find(".xsubmit").click(function(){
					form.submit();
				});
			},
			submit:function(){
				$.ajax({
	                url:ops.url,
	                type:ops.method,
	                dataType:ops.dataType,
	                data:inside.getData(),
					success:inside.success,
					error:inside.error
	            });
			},
			success:function(d){
				if(ops.success)ops.success(d);
			},
			error:function(e){
				if(ops.error)ops.error(e);
            	else $.error("保存失败！");
			},
			getData:function(){
				var data = [];
				var form = ths[0];
				var formId = ths.attr('id');
			    var els = ops.semantic ? form.getElementsByTagName('*') : form.elements;
			    if (els && !/MSIE [678]/.test(navigator.userAgent)) {
			        els = $(els).get();
			    }
			    if (!els || !els.length) {
			        return data;
			    }
			    var i,j,n,v,el,max,jmax;
			    for(i=0, max=els.length; i < max; i++) {
			        el = els[i];
			        n = el.name;
			        if (!n || el.disabled) {
			            continue;
			        }
			        v = inside.fieldValue(el,true);
			        if (v && v.constructor == Array) {
			            for(j=0, jmax=v.length; j < jmax; j++) {
			            	data.push({name: n, value: v[j]});
			            }
			        }
			        else if (inside.fileapi && el.type == 'file') {
			            var files = el.files;
			            if (files.length) {
			                for (j=0; j < files.length; j++) {
			                	data.push({name: n, value: files[j], type: el.type});
			                }
			            }
			        }
			        else if (v !== null && typeof v != 'undefined') {
			        	data.push({name: n, value: v, type: el.type, required: el.required});
			        }
			    }
			    return data;
			},
			fieldValue:function(el, successful) {
			    var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
			    if (successful === undefined) {
			        successful = true;
			    }

			    if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
			        (t == 'checkbox' || t == 'radio') && !el.checked ||
			        (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
			        tag == 'select' && el.selectedIndex == -1)) {
			            return null;
			    }

			    if (tag == 'select') {
			        var index = el.selectedIndex;
			        if (index < 0) {
			            return null;
			        }
			        var a = [], ops = el.options;
			        var one = (t == 'select-one');
			        var max = (one ? index+1 : ops.length);
			        for(var i=(one ? index : 0); i < max; i++) {
			            var op = ops[i];
			            if (op.selected) {
			                var v = op.value;
			                if (!v) {
			                    v = (op.attributes && op.attributes.value && !(op.attributes.value.specified)) ? op.text : op.value;
			                }
			                if (one) {
			                    return v;
			                }
			                a.push(v);
			            }
			        }
			        return a;
			    }
			    return $(el).val();
			}
		};
		var formdata = {
			submit:function(){
		        var fdata = new FormData();
		        var data = inside.getData();
		        for (var i=0; i < data.length; i++) {
		        	fdata.append(data[i].name, data[i].value);
		        }
	        	for(var n in ops.data){
	        		fdata.append(n, ops.data[n]);
	    		}
	        	formdata.doSubmit(fdata);
			},
			doSubmit:function(formdata){
		        $.ajax({
	                url:ops.url,
	                type:ops.method,
					success:inside.success,
					error:inside.error,
					contentType: false,
		            processData: false,
		            cache: false,
		            data:formdata
		        });
			}
		}
		var iframe = {
			id:null,
			io:null,
			checkCount:20,
			submit:function(){
				iframe.id = 'xui_form_' + (new Date().getTime());
				iframe.io = $('<iframe id="' + iframe.id + '" name="' + iframe.id + '"/>').appendTo('body');
				iframe.io.attr('src', window.ActiveXObject ? 'javascript:false' : 'about:blank');
				iframe.io.css({ position: 'absolute', top: '-1000px', left: '-1000px' });
				iframe.io.bind('load', iframe.cb);
				iframe.doSubmit();
			},
			doSubmit:function(){
				ths.attr('action', ops.url);
				var t = ths.attr('target');
				ths.attr('target', iframe.id);
				var paramFields = $();
				try {
					for(var n in ops.data){
						var field = $('<input type="hidden" name="' + n + '">').val(ops.data[n]).appendTo(ths);
						paramFields = paramFields.add(field);
					}
					iframe.checkState();
					ths[0].submit();
				} finally {
					ths.attr('action', ops.url);
					t ? ths.attr('target', t) : ths.removeAttr('target');
					paramFields.remove();
				}
			},
			cb:function(){
				iframe.io.unbind();
				var data = '';
				try{
					var body = iframe.io.contents().find('body');
					data = body.html();
					if (data == ''){
						if (--iframe.checkCount){
							setTimeout(iframe.cb, 200);
							return;
						}
					}
					var ta = body.find('>textarea');
					if (ta.length){
						data = ta.val();
					} else {
						var pre = body.find('>pre');
						if (pre.length){
							data = pre.html();
						}
					}
				} catch(e){
				}
				ops.success(data);
				setTimeout(function(){ iframe.io.unbind(); iframe.io.remove(); }, 200);
			},
			checkState:function(){
				var f = $('#'+iframe.id);
				$.log(f.length);
				if (!f.length){return}
				try{
					var s = f.contents()[0].readyState;
					if (s && s.toLowerCase() == 'uninitialized'){
						setTimeout(iframe.checkState, 100);
					}
				} catch(e){
					iframe.cb();
				}
			}
		}
		var xform = function(){
			inside.init();
		};
		var form = new xform();
		var outside = {
			submit:function(){
				if(ops.valid()){
					if(ops.fileupload&&inside.formdata){
						formdata.submit();
					}else if(ops.fileupload){
						iframe.submit();
					}else{
						inside.submit();
					}
				}
			},
			setData:function(data){
				$.each(data, function (name, ival) {
					var $input = ths.find("input[name = " + name + "]");
					if ($input.prop("type") == "radio" || prop.attr("type") == "checkbox") {
						$input.each(function(){
							if(Object.prototype.toString.apply(ival) == '[object Array]'){
								for (var i in ival) {
									if($(this).val() == ival[i]){
										$(this).prop("checked", true);
									}
								}
							} else{
								if($(this).val() == ival){
									$(this).prop("checked", true);
								}
							}
						});
					} else{
						ths.find("[name = "+name+"]").val(ival);
					}
					$input = ths.find("select[name = " + name + "]");
					if($input){
						$input.trigger("change");
					}
				});
			},
			setUrl:function(url){
				ops.url = url;
			},
			show:function(){
				ths.css("display","block");
				outside.resize();
			},
			hide:function(){
				ths.css("display","none");
			},
			resize:function(){
				var thsh = ths.find(".minwin_box").outerHeight();
				var thsw = ths.find(".minwin_box").outerWidth();
				var winh=$(window).outerHeight();
				if(thsh>winh){
					ths.find(".minwin_box").css({
						"max-height":(winh-50)+"px",
						"margin-top":"-"+(winh-50)/2+"px",
						"margin-left":"-"+(thsw)/2+"px"
					});
					ths.find(".minwin_box ul").css({
						"max-height":winh-174+"px",
		                "overflow":"auto"
		        	});
				}else{
					ths.find(".minwin_box").css({
						"margin-top":"-"+(thsh)/2+"px",
						"margin-left":"-"+(thsw)/2+"px"
					});
				}
			},
			clear:function(){
				ths.find("input[type = text],input[type = hidden],input[type = password],input[type = file],textarea").val("");
			},
			getData:function(){
				return inside.getData();
			}
		};
		$.extend(form,outside);
		return form;
    };
	$.fn.xwindow = function(options){
		var ths = this;
		if(ths.length==0){$.log("error: Unable to read element "+ths.selector);return;}
		var xfn = $(ths.selector).data(ths.selector);
		if(xfn){
			return xfn;
		}
		var defaults = {
			width: 500,
			height: 300,
			open:false
		}
		var ops = $.extend(defaults, options);
		var inside = {
			init:function(){
				inside.draw();
				inside.bind();
				if(ops.open){
					inside.open();
				}
			},
			draw:function(){
				ths.find(".xbox").css({
					"width":ops.width,
					"height":ops.height,
					"margin-top":-ops.height/2+'px',
					"margin-left":-ops.width/2+'px'
				});
			},
			bind:function(){
				ths.find(".close").click(function(){
					ths.hide();
				});
			},
			open:function(){
				ths.show();
			},
			close:function(){
				ths.hide();
			}
		}
		var xwindow = function(){
			inside.init();
		}
		var mywindow = new xwindow();
		var outside = {
			open:function(){
				inside.open();
			},
			close:function(){
				inside.close();
			},
			setWidth:function(w){
				ths.find(".xbox").css({"width":w+"px"});
			},
			setTitle:function(title){
				ths.find(".xbox").find(".title").eq(0).html(title);
			}
		}
		$.extend(mywindow,outside);
		$(ths.selector).data(ths.selector,mywindow);
		return mywindow;
	};
	$.fn.xcombo = function(options){
		var ths = this;
		if(ths.length==0){$.log("error: Unable to read element "+ths.selector);return;}
		var xfn = $(ths.selector).data(ths.selector);
		if(xfn){ return xfn; }
		var defaults = {
			url:null,
			data:null,
			width:200,
			height:30,
			xdata:null,
			selected:null,
			template:null,
			change:function(){},
			success:function(){}
		};
		var ops = $.extend(defaults,options,{
			tpl:null,
			template:null
		});
		var inside = {
			init:function(){
				inside.template();
				inside.load();
			},
			load:function(){
				if(ops.url){
					$.ajax({
						url:ops.url,
						data:ops.data,
						type:'POST',
						dataType:'json',
						success:inside.success,
						error:inside.error
					});
				}else if(ops.xdata){
					inside.draw(ops.xdata);
				}else{
	            	inside.bind();
	            	inside.call();
				}
			},
			template:function(){
				ths.width(ops.width);
				ths.height(ops.height);
				ops.tpl = ths.find(".xtemplate");
				if(ops.tpl.length>0){
					ops.template = ops.tpl.html();
					ops.tpl.empty();
					ops.tpl.css("width",ops.width);
				}else{
					ths.find("ul").css("width",ops.width);
				}
			},
			success:function(data){
				ops.xdata = data;
				inside.draw(data);
			},
			draw:function(){
            	ops.tpl.empty();
            	$.each(ops.xdata,function(index,row){
            		row.index = index;
            		ops.tpl.append($.xtemplate(ops.template,row));
            	});
            	if(ops.selected){
            		inside.val(ops.selected);
            	}
            	inside.bind();
            	inside.call();
			},
			error:function(e){
				$.error("数据加载失败！")
			},
			bind:function(){
				ths.unbind("click").click(function(){
					ths.find("ul").toggle();
				});
				$(document).mousedown(function(event) {
					if ($(event.target).parents().hasClass("xcombo") == false) {
						ths.find("ul").hide();
					}
				});
				ths.find("li").unbind("click").click(function(){
					inside.itemClick(this);
				})
			},
			call:function(){
				if(ops.success){
            		ops.success();
            	}
			},
			itemClick:function(item){
				var value = $(item).attr("xval");
				var text = $(item).html();
				ths.find("input[type = hidden]").val(value);
				ths.find(".xtext").html(text);
				ths.find("li").removeClass("selected");
				$(item).addClass("selected");
				if(ops.change){ops.change(value,text);};
			},
			val:function(v){
				ths.find("ul").children().each(function(index,item){
					var value = $(item).attr("xval");
					if(v == value){
						inside.itemClick(item);
					}
				});
			}
		};
		var xcombo = function(){
			inside.init();
		};
		var combo = new xcombo();		
		var outside = {
			setData:function(data){
				ops.xdata = data;
				inside.load();
			},
			getData:function(){
				return ops.xdata;
			},
			getRow:function(index){
				return ops.xdata[index];
			},
			select:function(i){
				ths.find("ul").children().each(function(index,item){
					if(i == index){
						inside.itemClick(item);
					}
				});
				return this;
			},
			val:function(v){
				if(v != undefined){
					inside.val(v);
				}else{
					return ths.find("input[type = hidden]").val();
				}
			},
			clear:function(){
				ths.find("li").removeClass("selected");
				ths.find(".xtext").html("请选择");
				ths.find("input[type = hidden]").val("");
				return this;
			},
			reload:function(data){
				$.extend(ops.data,data);
				inside.load();
			}
		};
		$.extend(combo,outside);
		$(ths.selector).data(ths.selector,combo);
		return combo;
	};
	$.fn.xaccordion = function(options){
		var ths = this;
		var defaults = {
			url:null,
			data:null,
			xdata:null,
			width:'100%',
			height:'100%',
			basePath:"",
			format:function(row){},
			success:function(){}
		};
		var ops = $.extend(defaults,options,{
			tpl:null,
			template:null,
			template_son:null
		});
		var inside = {
			init:function(){
				inside.template();
				inside.setSize();
				inside.load();
			},
			template:function(){
				ops.tpl = ths.find(".xtemplate");
				ops.template = ops.tpl.html();
				ops.template_son = ths.find(".xtemplate_son").html();
				ths.find(".xtemplate_son").remove();
			},
			setSize:function(){
				ths.width(ops.width);
				ths.height(ops.height);
			},
			load:function(){
				if(ops.url){
					$.ajax({
						url:ops.url,
						data:ops.data,
						type:'get',
						dataType:'json',
						success:inside.success,
						error:inside.error
					});
				}else if(ops.xdata){
					inside.draw(ops.xdata);
				}
			},
			success:function(data){
				inside.draw(data.data);
			},
			error:function(){
				$.error("数据加载失败！");
			},
			draw:function(data){
				ops.xdata = data;
            	ops.tpl.empty();
				ops.tpl.show();
            	$.each(ops.xdata,function(index,row){
            		row.index = index;
            		ops.format(row);
            		row.xson=inside.getSon(row.son);
        			ops.tpl.append($.xtemplate(ops.template,row));
            	});
            	inside.bind();
            	inside.call();
			},
			getSon:function(son){
				var html = "";
				$.each(son,function(index,row){
					html += $.xtemplate(ops.template_son,row)
				});
				return html;
			},
			bind:function(){
				ths.find(".xparent").click(function(){
					ths.find(".xson").hide();
					$(this).find(".xson").show();
				});
			},
			call:function(){
				ops.success();
			}
		};
		var xaccordion = function(){
			inside.init();
		};
		var accordion = new xaccordion();
		var outside = {
				
		};
		$.extend(accordion,outside);
		return accordion;
	};
	
	$.fn.xbtn = function(options){
		var ths = this;
		var defaults = {
			click:function(){}
		};
		var ops = null;
		if(typeof options === 'function'){
			defaults.click = options;
			ops = $.extend(defaults, {
				btn:true,
				close:function(){ops.btn=false;},
				open:function(){ops.btn=true;}
			});
		}else{
			ops = $.extend(defaults, options, {
				btn:true,
				close:function(){ops.btn=false;},
				open:function(){ops.btn=true;}
			});
		}
		var inside = {
			init:function(){
				ths.unbind("click").click(function(event){
					event.stopPropagation();
					ops.xval = $(this).attr("xval");
					if(ops.btn){
						ops.click(ops);
					}
				});
			}
		};
		var xbtn = function(){
			inside.init();
		};
		btn = new xbtn();
		return btn;
	};
	$.xtemplate = function (template, data) {
		return template.replace(/\{([\w\.]*)\}/g, function(str, key) {
			var keys = key.split("."), v = data[keys.shift()];
			for (var i = 0, l = keys.length; i < l; i++) v = v[keys[i]];
			return (typeof v !== "undefined" && v !== null) ? v : "";
		});
	};
	$.format = function(str){
		var args = arguments, re = new RegExp("%([1-" + args.length + "])", "g");
		return String(str).replace( re, function($1, $2) {
			return args[$2];
		});
	}
	$.log = function(content){
		if (window.console && window.console.log){
			window.console.log(content);
		}
	};
	$.error = function(e,time,css){
		$(".msg_win").remove();
		var time = time||5000;
		var css = css==null?"style='top:0'":"style='"+css+"'";
		var dom = "<div class='msg_win' "+css+"><div class='error_box'>"+e+"</div></div>";
		var xno = $(dom).appendTo("body");
		xno.fadeOut(time,function(){
			xno.remove();
		});
	};
	$.ok = function(e,time,css){
		$(".msg_win").remove();
		var time = time||4000;
		var css = css==null?"style='top:0'":"style='"+css+"'";
		var dom = "<div class='msg_win' "+css+"><div class='ok_box'>"+e+"</div></div>";
		var xno = $(dom).appendTo("body");
		xno.fadeOut(time,function(){
			xno.remove();
		});
	};
	$.xalert = function(options) {
		var defaults = {
			id:'xalert',
			title:'信息提示',
			ok:"确定",
			no:"取消",
			msg:'',
			cls:'blue',
			width: 260,
			height : 120,
			confirm : false,
			okBack:function(){},
			noBack:function(){}
		};
		var ops = $.extend(defaults, options);
		ops.winId = ops.id+'_win';
		ops.coverId = ops.id+'_cove';
		ops.okId = ops.id+'_ok';
		ops.noId = ops.id+'_no';
		var inside = {
			init:function(){
				var buttons = '<div id="'+ops.okId+'">'+ops.ok+'</div>';
				if(ops.confirm){
					buttons = '<div id="'+ops.noId+'" class="no">'+ops.no+'</div><div id="'+ops.okId+'" class="ok right">'+ops.ok+'</div>';
				}
				var dom = '<div id="'+ops.coverId+'" class="xcover"></div>'+
				'<div id="'+ops.winId+'" class="xalert '+ops.cls+'">'+
				'<div class="title">'+ops.title+'</div>'+
				'<div class="content">'+ops.msg+'</div>'+
				'<div class="buttons">'+buttons+'</div>'+				
				'</div>';
				$("body").append(dom);
				$("#"+ops.winId).css({
					"width":ops.width,
					"height":ops.height,
					"margin-top":-ops.height/2+'px',
					"margin-left":-ops.width/2+'px'
				});
			},
			bind:function(){
				$("#"+ops.okId).click(function(){
					outside.close();
					ops.okBack();
				});
				$("#"+ops.noId).click(function(){
					outside.close();
					ops.noBack();
				});
			},
			open:function(){
				$("#"+ops.winId).show();
				$("#"+ops.coverId).show();
			}
		}
		var xalert =  function(){
			inside.init();
			inside.bind();
			inside.open();
		}
		var alert = new xalert();
		var outside = {
			open:function(){
				inside.open();
			},
			close:function(){
				$("#"+ops.winId).remove();
				$("#"+ops.coverId).remove();
			}
		}
		$.extend(alert,outside);
		return alert;
	};
})(jQuery);